//
//  COMoneyHTTP.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/30.
//

import UIKit
import Moya
import HDeviceIdentifier

let moneyProvider = MoyaProvider<COMoneyHTTP>()
enum COMoneyHTTP {
    case list
    case pay(orderId: String, receiptData: String)
}

extension COMoneyHTTP: TargetType {
    var path: String {
        switch self {
        case .list:
            return "/coco/coco589/apple/110135"
        case .pay:
            return "/coco/coco589/apple/pay"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .list:
            return .get
        case .pay:
            return .post
        }
    }
    
    var task: Moya.Task {
        var params: [String: Any] = [:]
        switch self {
        case .list:
            params["uuId"] = HDeviceIdentifier.deviceIdentifier()
            return .requestParameters(parameters: params, encoding: URLEncoding.default)
        case let .pay(orderId, receiptData):
            params["uuId"] = HDeviceIdentifier.deviceIdentifier()
            params["appleOrderId"] = orderId
            params["receiptData"] = receiptData
        }
        
        return .requestParameters(parameters: params, encoding: JSONEncoding.default)
    }
    
    var headers: [String : String]? {
        var params: [String: String] = [:]
        params["AVS"] = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? ""
        params["OSB"] = "1"
        params["AGKB"] = "H1"
        params["token"] = COUserManager.userInfo?.token
        return params
    }
}
